import { ApiProperty, getSchemaPath } from '@nestjs/swagger';
import { CrudValidationGroups } from '@nestjsx/crud';
import {
    IsOptional, IsString,
    ValidateNested
} from 'class-validator';
import {
    Column,
    Entity,
    ManyToOne,
    PrimaryGeneratedColumn
} from 'typeorm';
import { file } from './about/file';
import { BaseEntity } from './base.entity';
import { Group } from './group.entity';
import { OrgActionType } from './org-action-type.entity';
import { Village } from './village.entity';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity()
export class OrgAction extends BaseEntity {
  @ApiProperty({
    description: '党组织活动',
    required: false,
  })
  @PrimaryGeneratedColumn()
  id?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number',
      },
      {
        $ref: getSchemaPath(() => Village),
      },
    ],
  })
  @ManyToOne(
    () => Village,
    Village => Village.orgActionType,
  )
  village: Number | Village;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.orgAction,
    {
      eager: true,
    },
  )
  group: Number | Group;
  

  @ApiProperty({
    example: 1,
    description: '党组织活动类型',
    required: true,
    oneOf: [
      {
        type: 'Number',
      },
      {
        $ref: getSchemaPath(() => OrgActionType),
      },
    ],
  })
  @ManyToOne(
    () => OrgActionType,
    OrgActionType => OrgActionType.orgAction,
    {
      eager: true,
    },
  )
  type: number | OrgActionType;

  @ApiProperty({
    example: '党组织活动名字',
    description: '党组织活动名字',
    required: true,
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string;

  @ApiProperty({
    example: '党组织活动时间',
    description: '党组织活动时间',
    required: true,
  })
  @IsString()
  @Column({
    type: "date",
  })
  time: Date;

  @ApiProperty({
    example: '党组织活动-地点',
    description: '党组织活动-地点',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    nullable: true,
    type: "char",
    length: 255,
  })
  location: string;

  @ApiProperty({
    example: '党组织活动-内容',
    description: '党组织活动-内容',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true,
  })
  content: string;

  @ApiProperty({
    example: '党组织活动-相关文件',
    description: '党组织活动-相关文件',
    required: true,
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  relatedDocuments: file [];

  @ApiProperty({
    example: '参与人员',
    description: '参与人员',
    required: false,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: 'text',
    nullable: true
  })
  actor: string;
}
